'''
主要生成对应的索引树，便于快速查找,jsonpath
'''
from util.read_data import read_data
import jsonpath


def analysis_json(json_obj, exp):
    res = jsonpath.jsonpath(json_obj, exp)
    return res


# 临时版key_to_value反查
def key_to_value1(value):
    json_data = read_data('53info.json')
    for i in json_data.keys():
        for j in json_data[i].keys():
            for index, k in enumerate(json_data[i][j]['detail_url_list']):
                if value == k['adress']:
                    # print(i, j, index)
                    return i, j, index


# key_to_value方法抽象编写
def key_to_value(json_obj, value):
    '''
    通过字典的value反查key,并返回一个可用的字典查找层级
    :param json_obj: 字典对象
    :param value: 需要查找的值
    :return:
    '''
    key_list = []
    for var1 in json_obj.keys():
        if isinstance(json_obj[var1], str):
            if json_obj[var1] == value:
                print('str var1>>>>>>>>>>>', key_list)
                return var1
        elif isinstance(json_obj[var1], list):
            for index, var2 in enumerate(json_obj[var1]):
                key_to_value(var2, value)
        elif isinstance(json_obj[var1], dict):
            if value in json_obj[var1].values():
                print('&&&&&&&&')
            key_to_value(json_obj[var1], value)


if __name__ == '__main__':
    json_data = read_data('53info.json')
    # analysis_json(json_data, '$.建筑工程')
    c, cl, index = key_to_value1('http://www.53kjw.com/lcjyjs/8314.html')
    print(json_data[c][cl])